package portal.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import portal.vo.ProductoVO;
import portal.web.businessDelegate.PortalComprasDelegate;
import portal.web.utils.EncodingUtil;

@SuppressWarnings("serial")
public class AutoCompleteServlet extends HttpServlet {

	private static Logger logger = Logger.getLogger(AutoCompleteServlet.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		processRequest(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		processRequest(req, resp);
	}

	private void processRequest(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>AutoCompleteServlet<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
		
		resp.setContentType("text/html");
		PrintWriter out= resp.getWriter();
		String nombre= EncodingUtil.fixEncoding(req.getParameter("q"));
		List<ProductoVO> listado = new ArrayList<ProductoVO>();
		listado = PortalComprasDelegate.getInstance().getListaProductosByNombre(nombre);
		//List<String> name = getData(nm);

		Iterator<ProductoVO> itr = listado.iterator();
		while(itr.hasNext()) {
			String cadena = (String)itr.next().getNombre();
			out.println(cadena);
		}

		out.close();

	}
}
